#!/bin/bash
##
# Date: 2018-03-06
# Author: phiix
# Describe: compile install php
##
trap "{ echo interrupt by keyboard Ctrl-C;exit 1 ; }" SIGINT

export PATH=/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export LANG=en_US.UTF-8

current_dir=`pwd`
config_path="${current_dir%/*}/configs/php"
install_log="/root/php_install.log"
php_user=wwwpub
php_version=''

check_env () {
echo "Install complie tools and dependency packages."
if [ ! -f /etc/yum.repos.d/epel.repo ];then
	cp ${current_dir%/*}/yum.repos.d/epel.repo /etc/yum.repos.d/
fi
yum install -y bzip2 gcc gcc-c++
yum install -y libxml2-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel openssl-devel
}

add_php_user () {
id ${php_user} >/dev/null 2>&1
if [ $? -ne 0 ];then
	groupadd -r ${php_user}
	useradd -r -g ${php_user} -s /usr/sbin/nologin ${php_user}
fi
}

# Get php version
cd /usr/local/src
index=1
for php_source in `find * -name "php-*.tar.*" -print`
do
	php[index++]=${php_source}
done
if [ "x${php[1]}" == "x" ];then
	echo "No available php source package."
	echo "Please put the php source packages to /usr/local/src directory."
	echo "Exit..."
	exit 1
fi
echo "Available php source packages list:"
for index in ${!php[@]}
do
	echo "$index) ${php[index]}"
done
echo "Choce the php version to install."
read -t 15 choice
if [[ $choice =~ [1-9][0-9]* ]] && [ $choice -le ${#php[@]} ];then
	php_version=${php[choice]%.tar*}
	echo "Choice php version is ${php_version} ."
else
	echo "Choice error.Exit..."
	exit 1
fi

cd /usr/local
ls |grep -Pq ${php_version}
if [ $? -eq 0 ];then
	echo "${php_version} has been installed.Exit..."
	exit 1
fi

clear
echo "--->Start install ${php_version}<---"
check_env
add_php_user

cd /usr/local/src
[ -f ${php[choice]} ] && tar -xvf ${php[choice]}

cd ${php_version}
compile_72="--prefix=/usr/local/${php_version} \
--with-config-file-path=/usr/local/${php_version}/etc \
--enable-fpm \
--enable-exif \
--enable-sockets \
--enable-bcmath \
--enable-mbstring \
--enable-pcntl \
--enable-calendar \
--enable-opcache \
--enable-zip \
--with-fpm-user=${php_user} \
--with-fpm-group=${php_user} \
--with-mysqli=mysqlnd \
--with-pdo-mysql \
--enable-mysqlnd \
--with-zlib \
--with-gd \
--with-png-dir \
--with-jpeg-dir \
--with-gettext \
--with-freetype-dir \
--with-openssl \
--with-curl "

compile_71="--prefix=/usr/local/${php_version} \
--with-config-file-path=/usr/local/${php_version}/etc \
--enable-fpm \
--enable-exif \
--enable-sockets \
--enable-bcmath \
--enable-mbstring \
--enable-pcntl \
--enable-calendar \
--enable-opcache \
--enable-zip \
--with-fpm-user=${php_user} \
--with-fpm-group=${php_user} \
--with-mysqli=mysqlnd \
--with-pdo-mysql \
--enable-mysqlnd \
--with-zlib \
--with-gd \
--with-png-dir \
--with-jpeg-dir \
--with-gettext \
--with-freetype-dir \
--with-mcrypt \
--with-openssl \
--with-curl "

cpu_proc=`grep proc /proc/cpuinfo|wc -l`

if [[ "${php_version%.*}" != "php-7.2" ]];then
	./configure ${compile_71} && make -j ${cpu_proc} && make install || result=1
else
	./configure ${compile_72} && make -j ${cpu_proc} && make install || result=1
fi

if [ ${result:-0} -ne 0 ];then
	echo "Compile error.Exit..."
	exit 1
fi

if [ -d /etc/php ] || [ -L /etc/php ];then
	cd /etc/php
	cp -a php-fpm.conf php.ini /usr/local/${php_version}/etc/
	cp -a php-fpm.d/www.conf /usr/local/${php_version}/etc/php-fpm.d/
fi

mkdir -p /var/log/php

switch_version () {
cd /usr/local
[ -L php ] && rm -f php
[ -d php ] && mv php php.`date +%F`
[ -L /etc/php ] && rm -f /etc/php
[ -d /etc/php ] && mv /etc/php /etc/php.`date +%F`

ln -s ${php_version} php
ln -s /usr/local/php/etc /etc/php
ln -sf /usr/local/php/sbin/* /usr/local/sbin/
ln -sf /usr/local/php/bin/*  /usr/local/bin/

cd /usr/lib/systemd/system
[ -f php-fpm.service ] && mv php-fpm.service php-fpm.service.`date +%F`
cp ${config_path}/php-fpm.service /usr/lib/systemd/system/

php -v 
php -m

echo "Excute: systemctl start php-fpm ,to start php-fpm.service"
echo "Excute: systemctl enable php-fpm .to add php-fpm.service start when system started"
}

read -t 15 -p "Switch php version ? [y/n](default: n)  " reply
if [ "${reply:-n}" == "y" ];then
	switch_version
else
	echo "Do not switch php version."
fi

echo -e "\n--->Install completed,OK!<---"
exit 0









